<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/11
 * Time: 下午3:15
 */

namespace App\WeChat\WxApi;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class WxKefuApi extends WxApi
{
    /**
     * 添加客服账号
     * @param $kf_account
     * @param $nickname
     * @param $password (md5 password)
     * @return mixed({"errcode" : 0,"errmsg" : "ok"})
     * @throws \Exception
     */
    public static function add($kf_account, $nickname, $password){
        $data = json_encode([
            'kf_account'=>$kf_account,
            'nickname'=>$nickname,
            'password'=>$password
        ]);

        $uri = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $data);
        $client = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
             return $response->getBody();
        }

        return false;
    }

    /**
     * 修改客服账号
     * @param $kf_account
     * @param $nickname
     * @param $password
     * @return mixed
     * @throws \Exception
     */
    public static function update($kf_account, $nickname, $password){
        $data = json_encode([
            'kf_account'=>$kf_account,
            'nickname'=>$nickname,
            'password'=>$password
        ]);
        $uri = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $data);
        $client = new Client();
        $response = $client->send($request);

        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }

        return false;
    }

    /**
     * 删除客服账号
     * @param $kf_account
     * @param $nickname
     * @param $password
     * @return mixed
     * @throws \Exception
     */
    public static function delete($kf_account, $nickname, $password){
        $data = json_encode([
            'kf_account'=>$kf_account,
            'nickname'=>$nickname,
            'password'=>$password
        ]);

        $uri = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $data);
        $client = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * 上传客服头像
     * @param $kf_account
     * @param $imgfile --头像绝对路径
     * @return mixed
     * @throws \Exception
     */
    public static function uploadHeadimg($kf_account, $imgfile){
        $access_token = self::getAccessToken();
        $media = new \CURLFile($imgfile);
        $access_data = array('media'=>$media);
        $url = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=$access_token&kf_account=$kf_account";
        return Http::curlPost($url, $access_data);
    }

    /**
     * 获取客服列表
     * @return mixed
     * @throws \Exception
     */
    public static function getKfList(){
        $uri = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=".self::getAccessToken();
        $client = new Client();
        $response = $client->get($uri);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }
}
